package com.sun.app.service.app;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.http.HttpServletRequest;

import com.sun.app.service.BaseService;
import com.sun.core.Constants;
import com.sun.core.util.PagingUtil;
import com.sun.util.AppResult;
import com.sun.util.TimeUtil;

public class RoundService extends BaseService{

	@Override
	public Integer add(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer delete(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer update(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}
	
	//得到所有系统一级分类标签 Status未0
	private static final String SELECT_TIP_BY_UID = "select distinct TIPID,TIPNAME from c_tip where STATE=0 and PARENTID=0 order by LVL DESC,COUNT DESC";
	public List<Map<String,Object>> getTipByUID(){
		return jt.queryForList(SELECT_TIP_BY_UID);
	}
	//得到标签List 登陆用户得到自己标签下的子标签List 未登陆用户得到所有标签下的子标签
	private static final String SELECT_TIPNAME_BY_UID = "select t.TIPID,TIPNAME from c_tip t,c_user_tip u where u.UID=? and u.TIP=t.TIPID order by LVL DESC,COUNT DESC";
	private static final String SELECT_TIPNAME_BY_UID_UNLOGIN = "select TIPID,TIPNAME from c_tip t where PARENTID=(select TIPID from c_tip where STATE=0 and PARENTID=0 order by LVL DESC,COUNT DESC) group by TIPID order by LVL DESC,COUNT DESC";
	public List<Map<String,Object>> getTipByUID(String UID){
		if(UID==null){
			return jt.queryForList(SELECT_TIPNAME_BY_UID_UNLOGIN);
		}
		return jt.queryForList(SELECT_TIPNAME_BY_UID,UID);
	}
	
	//得到推荐圈子
	private static final String SELECT_ADVICEROUND_BY_TIPID = "select ROUNDNAME,r.ROUNDID,RPATH,COUNT from C_ROUND_PHOTO p,C_ROUND r,C_ROUND_TIP t where p.RID=r.ROUNDID and r.ROUNDID=t.ROUNDID and TIPID=? and r.STATUS = ? order by TOP DESC,COUNT DESC limit 0,12";
	private static final String SELECT_ADVICEROUND_UNLOGIN = "select ROUNDNAME,r.ROUNDID,RPATH,COUNT from C_ROUND_PHOTO p,C_ROUND r,C_ROUND_TIP t where p.RID=r.ROUNDID and r.ROUNDID=t.ROUNDID and r.STATUS = ? order by TOP DESC,COUNT DESC limit 0,12";
	public List<Map<String,Object>> getAdviceRound(HttpServletRequest request){
		String UID = (String) request.getSession().getAttribute("UID");
		//STATUS是系统圈子，根据round1页链接而来
		Integer STATUS=0;
		List<Map<String, Object>> tipList = new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> adviceRoundList = new LinkedList<Map<String,Object>>();
//		List<Map<String,Object>> RList = new ArrayList<Map<String,Object>>();
		//通过传递STATUS从而改变推荐活动信息
		if(request.getAttribute("STATUS")!=null){
			STATUS=Integer.valueOf((String) request.getAttribute("STATUS"));
		}
		if(UID==null){
			//未登陆用户的推荐活动
			adviceRoundList = jt.queryForList(SELECT_ADVICEROUND_UNLOGIN,STATUS);
		}else{
			//登陆用户、给其对应标签下的圈子
			tipList = getTipByUID(UID);
			Iterator<Map<String, Object>> iterator = tipList.iterator();
			while(iterator.hasNext()){
				Map<String, Object> next = iterator.next();
				int tipId = (Integer) next.get("TIPID");
				System.out.println();
				System.out.println(tipId);
				List<Map<String, Object>> queryForList = jt.queryForList(SELECT_ADVICEROUND_BY_TIPID,tipId,STATUS);
				adviceRoundList.addAll(queryForList);
			}
			//随机获取推荐圈子
//			int size = adviceRoundList.size();
//			if(size>12){
//				//随机获取数据
//				for(int i=0;i<12;i++){
//					int s = size;
//					int random = (int)(Math.random() * s);
//					RList.add(adviceRoundList.get(random));
//					adviceRoundList.remove(random);
//					s--;
//				}
//				return RList;
//			}
		}
		

		return adviceRoundList;
	}
	
	//通过点击标签获取 圈子列表
	private static final String SELECT_ROUNDLIST_BY_PARENTID = "select ROUNDNAME,r.ROUNDID,RPATH,COUNT from C_ROUND_PHOTO p,C_ROUND r,C_ROUND_TIP t where p.RID=r.ROUNDID and r.ROUNDID=t.ROUNDID  and r.STATUS = 0 and TIPID in (select TIPID from c_tip where PARENTID=?) order by TOP DESC,COUNT DESC limit ?,?";
	public List<Map<String,Object>> getPublicRoundList(int tipParentId,int index,int size){
		return jt.queryForList(SELECT_ROUNDLIST_BY_PARENTID,tipParentId,index,size);
	}
	//得到圈子信息 By RID
	private static final String SELECT_ROUND_DETAIL = "select r.ROUNDID,r.ROUNDNAME,r.UID,r.STATUS,r.COUNT,r.DETAIL,rp.RPATH,u.USERTRUENAME,cp.MPATH from C_ROUND r,c_round_photo rp,c_user u,c_user_hdphoto cp where cp.UID=u.UID and rp.RID=r.ROUNDID and u.UID=r.UID and ROUNDID = ?"; 
	public Map<String,Object> getRoundByPK(int RoundId){
		List<Map<String, Object>> queryForList = jt.queryForList(SELECT_ROUND_DETAIL,RoundId);
		if(queryForList.isEmpty()){
			return new HashMap<String, Object>();
		}
		return queryForList.get(0);
	}
	//得到该圈子的管理员
	private static final String SELECT_MANANGER_FROM_ROUND = "select u.USERTRUENAME,u.UID,up.SPATH from c_user u,c_round_attention ca,c_user_hdphoto up where u.UID=ca.UID and up.UID=u.UID and ca.ROLE=2 and ca.ROUNDID=?";
	public List<Map<String,Object>> getManagerPeople(int roundId){
		return jt.queryForList(SELECT_MANANGER_FROM_ROUND,roundId);
	}
	//得到参加圈子的人
	private static final String SELECT_HUMAN_FROM_ROUND = "select ra.UID,SPATH from c_round_attention ra,c_user_hdphoto up where ROUNDID = ? and ra.UID=up.UID limit ?,?";
	//得到参加圈子的人的记录数 For 分页
	private static final String COUNT_HUMAN_FROM_ROUND = "select COUNT(*) from c_round_attention ra,c_user_hdphoto up where ROUNDID = ? and ra.UID=up.UID";
	public Map<String,Object> getPeoPle(HttpServletRequest request){
		Integer RoundId = 0;
		if(request.getAttribute("roundId")!=null){
			RoundId=Integer.valueOf((String)request.getAttribute("roundId"));
		}
		PagingUtil pagingUtil = new PagingUtil(request);
		int MaxSize = jt.queryForInt(COUNT_HUMAN_FROM_ROUND,RoundId);
		int[] limitCause = pagingUtil.getLimitCause("currentPage",12,MaxSize);
		List<Map<String, Object>> people = jt.queryForList(SELECT_HUMAN_FROM_ROUND,RoundId,limitCause[0],limitCause[1]);
		Map<String,Object> m = new HashMap<String, Object>();
		m.put("people", people);
		m.put("maxSize", limitCause[2]);
		return m;
	}
	//得到参加人所参加的圈子
	public Map<String,Object> getPeoPleAttend(HttpServletRequest request){
		List<Map<String, Object>> peoPle = (List<Map<String, Object>>) getPeoPle(request).get("people");
		Iterator<Map<String, Object>> it = peoPle.iterator();
		StringBuffer buf = new StringBuffer("");
		if(!it.hasNext()){
			buf.append("''");
		}
		while(it.hasNext()){
			Map<String, Object> next = it.next();
			String UID = (String) next.get("UID");
			buf.append("'"+UID+"'");
			if(it.hasNext()){
				buf.append(",");
			}
		}
		PagingUtil pagingUtil = new PagingUtil(request);
		String COUNT_HUMAN_ROUND = "select COUNT(distinct ca.ROUNDID) from c_round_attention ca,c_round r where r.ROUNDID=ca.ROUNDID and r.STATUS=0 and ca.UID in ("+buf+")";
		int MaxSize = jt.queryForInt(COUNT_HUMAN_ROUND);
		int[] limitCause = pagingUtil.getLimitCause("currentPage", 3,MaxSize);
		String SELECT_HUMAN_ROUND = "select ca.ROUNDID,RPATH,Count(ca.UID) as Count,r.ROUNDNAME,r.DETAIL from c_round_photo rp,c_round_attention ca,c_round r where r.ROUNDID=rp.RID and r.ROUNDID=ca.ROUNDID and r.STATUS=0 and ca.UID in ("+buf+") group by ca.ROUNDID order by Count(ca.UID) desc limit "+limitCause[0]+","+limitCause[1];
		List<Map<String, Object>> peopleAttend = jt.queryForList(SELECT_HUMAN_ROUND);
		Map<String,Object> m = new HashMap<String, Object>();
		m.put("peopleAttend", peopleAttend);
		m.put("maxSize", limitCause[2]);
		return m;
	}
	//得到公共所有推荐圈子
	public AppResult getAdviceRoundList(HttpServletRequest request){
		AppResult r = new AppResult();
		Map<String,Object> m = new HashMap<String, Object>();
		List<Map<String, Object>> adviceRoundList = getAdviceRound(request);
		List<Map<String, Object>> tipList = getTipByUID();
		m.put("adviceRoundList", adviceRoundList);
		m.put("adviceRoundListSize", adviceRoundList.size());
		m.put("tipList", tipList);
		m.put("tipListSize", tipList.size());
		r.setResult(m);
		r.setStatus(200);
		r.setMsg("success");
		return r;
	}
	//得到公共圈子分页 List By TipId
	private static final String COUNT_ROUNDLIST_BY_TIPID = "select COUNT(*) from C_ROUND_PHOTO p,C_ROUND r,C_ROUND_TIP t where p.RID=r.ROUNDID and r.ROUNDID=t.ROUNDID  and r.STATUS = 0 and TIPID=?";
	public AppResult getRoundListByTip(HttpServletRequest request){
		String tipstr = (String) request.getAttribute("tipId");
		int tipId = Integer.valueOf(tipstr);
		AppResult r = new AppResult();
		Map<String,Object> m = new HashMap<String, Object>();
		PagingUtil p = new PagingUtil(request);
		int MaxSize = jt.queryForInt(COUNT_ROUNDLIST_BY_TIPID,tipId);
		int[] limitCause = p.getLimitCause("num", Constants.PAGESIZE,MaxSize);
		List<Map<String, Object>> publicRoundList = getPublicRoundList(tipId,limitCause[0],limitCause[1]);
		m.put("publicRoundList", publicRoundList);
		m.put("publicRoundListSize", publicRoundList.size());
		r.setMsg("success");
		r.setStatus(200);
		r.setResult(m);
		return r;
	}
	//得到分享信息
	private static final String SELECT_THING_FROM_SHARE = "select s.UID,USERTRUENAME,SPATH,SHARETYPE,THINGID,DETAIL,(select ROUNDNAME from c_round where ROUNDID=FROMID) as ROUNDNAME,s.DATELINE from c_round_share s,c_user u,c_user_hdphoto up where u.UID=up.UID and u.UID=s.UID and ROUNDID=? order by s.DATELINE DESC";
	public List<Map<String,Object>> getInformation(HttpServletRequest request){
		String roundId = (String)request.getAttribute("roundId");
		if(roundId==null){
			roundId = "0";
		}
		List<Map<String,Object>> sList=new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> shareList = jt.queryForList(SELECT_THING_FROM_SHARE,roundId);
		Iterator<Map<String, Object>> it = shareList.iterator();
		while(it.hasNext()){
			Map<String, Object> next = it.next();
			int shareType = (Integer)next.get("SHARETYPE");
			int thingId = (Integer)next.get("THINGID");
			String fromName = (String)next.get("ROUNDNAME");
			Date d = (Date) next.get("DATELINE");
			String img = (String)next.get("SPATH");
			String adder = (String)next.get("USERTRUENAME");
			String UID = (String)next.get("UID");
			String DETAIL = (String)next.get("DETAIL");
			String shareDate = TimeUtil.dateToPointDatetime(d);
			System.out.println("啊"+shareType+"\n\n\n");
			switch(shareType){
			case 0 :
				/*
				 * 若数据库字段缺失、此处报错
				 */
				Map<String, Object> music = getMusic(thingId);
				music.put("SHARETYPE", shareType);
				music.put("fromName", fromName);
				music.put("shareDate", shareDate);
				music.put("img", img);
				music.put("adder", adder);
				music.put("shareUID", UID);
				music.put("remark", DETAIL);
				sList.add(music);
				break;
			case 1 :
				Map<String, Object> activity = getActivity(thingId);
				activity.put("SHARETYPE", shareType);
				activity.put("fromName", fromName);
				activity.put("shareDate", shareDate);
				activity.put("img", img);
				activity.put("adder", adder);
				activity.put("shareUID", UID);
				activity.put("remark", DETAIL);
				sList.add(activity);
				break;
			case 2 :
				Map<String, Object> content = getContent(thingId);
				content.put("SHARETYPE", shareType);
				content.put("fromName", fromName);
				content.put("shareDate", shareDate);
				content.put("img", img);
				content.put("adder", adder);
				content.put("shareUID", UID);
				content.put("remark", DETAIL);
				sList.add(content);
				break;
			case 3 :
				Map<String, Object> video = getVideo(thingId);
				video.put("SHARETYPE", shareType);
				video.put("fromName", fromName);
				video.put("shareDate", shareDate);
				video.put("img", img);
				video.put("adder", adder);
				video.put("shareUID", UID);
				video.put("remark", DETAIL);
				sList.add(video);
				break;
			}
		}
		return sList;
	}
	
	//得到视频
	private static final String SELECT_VIDEO_BY_PK = "select VIDEONAME,VIDEOPATH,v.UID,USERTRUENAME from c_information_video v,c_user u where v.UID=u.UID and VIDEOID=?";
	public Map<String,Object> getVideo(int videoId){
		return jt.queryForMap(SELECT_VIDEO_BY_PK,videoId);
	}
	//得到音乐
	private static final String SELECT_MUSIC_BY_PK = "select MUSICNAME,SINGER,DETAIL,MUSICPATH,IMGPATH,m.UID,USERTRUENAME from c_information_music m,c_user u where u.UID=m.UID and MUSICID=?";
	public Map<String,Object> getMusic(int musicId){
		return jt.queryForMap(SELECT_MUSIC_BY_PK,musicId);
	}
	//得到文字信息
	private static final String SELECT_CONTENT_BY_PK = "select CONTENTNAME,CONTENT,IMGPATH,c.UID,USERTRUENAME from c_information_content c,c_user u where u.UID=c.UID and CONTENTID = ?";
	public Map<String,Object> getContent(int contentId){
		return jt.queryForMap(SELECT_CONTENT_BY_PK,contentId);
	}
	//得到活动
	private static final String SELECT_ACTIVITY_BY_PK = "select APPATH,u.UID,ACTRANGE,AREANAME,CITYNAME,TIPNAME,STREET,TITLE,STREETDETAIL,BEGINTIME,ENDTIME,DETAIL,a.STATE,USERTRUENAME,AREANAME,a.ACTID from c_activity_photo ap,c_activity a,c_tip t,m_city c,c_area ar,c_user u " +
	"where ap.ACTID=a.ACTID and t.TIPID=a.TIPID and u.UID = a.UID and c.CODE = ar.CITYID and ar.AREAID=a.AREAID and a.ACTID=?";
	/*
	 * 通过活动ID查出该活动的关注人数
	 */
	private static final String COUNT_CARE_ACTIVITY = "select COUNT(*) from c_activity_attention where ACTID=?";
	public Integer getAcitivityCareCount(int ACTID){
		return jt.queryForInt(COUNT_CARE_ACTIVITY,ACTID);
	}
	public Map<String,Object> getActivity(int activityId){
		Map<String, Object> queryForMap = jt.queryForMap(SELECT_ACTIVITY_BY_PK,activityId);
		/*
		 * 对activity的日期做处理
		 */
		Date beginTime = (Date) queryForMap.get("BEGINTIME");
		Date endTime = (Date) queryForMap.get("ENDTIME");
		queryForMap.put("BEGINTIME", TimeUtil.DateFormatForAdvice(beginTime));
		queryForMap.put("ENDTIME", TimeUtil.DateFormatForAdvice(endTime));
		queryForMap.put("COUNT", getAcitivityCareCount(activityId));
		return queryForMap;
	}
	
	//获取好友参加的其他圈子
	public AppResult othersAttendRound(HttpServletRequest request){
		AppResult r = new AppResult();
		Map<String,Object> m = new HashMap<String, Object>();
		//requeset里存好友参加的其他圈子 前台page为currentPage
		Map<String, Object> mt = getPeoPleAttend(request);
		List<Map<String, Object>> peopleAttend = (List<Map<String, Object>>) mt.get("peopleAttend");
		int maxSize = (Integer) mt.get("maxSize");
		System.out.println(peopleAttend.size()+"\n"+maxSize);
		m.put("peopleAttend", peopleAttend);
		m.put("peopleAttendSize", peopleAttend.size());
		m.put("maxSize", maxSize);
		r.setMsg("success");
		r.setStatus(200);
		r.setResult(m);
		return r;
	}
	//得到推荐圈子的列表(左端)
	public AppResult adviceRoundInPublic(HttpServletRequest request){
		AppResult r = new AppResult();
		Map<String,Object> m = new HashMap<String, Object>();
		//requeset里存好友参加的其他圈子 前台page为advicePage
		Map<String, Object> s = getAdviceRoundInPublic(request);
		List<Map<String, Object>> adviceRound = (List<Map<String, Object>>) s.get("adviceList");
		int maxSize = (Integer)s.get("maxSize");
		m.put("maxSize", maxSize);
		m.put("adviceRound", adviceRound);
		m.put("adviceRoundSize", adviceRound.size());
		r.setMsg("success");
		r.setStatus(200);
		r.setResult(m);
		return r;
	}
	//得到分享信息
	public AppResult share(HttpServletRequest request){
		AppResult r = new AppResult();
		int RoundId = Integer.valueOf((String)request.getAttribute("roundId"));
		System.out.println(RoundId);
		Map<String,Object> m = new HashMap<String, Object>();
		//requeset里存好友参加的其他圈子 前台page为advicePage
		List<Map<String, Object>> share = getInformation(request);
		m.put("share", share);
		m.put("shareSize", share.size());
		r.setMsg("success");
		r.setStatus(200);
		r.setResult(m);
		return r;
	}

	//得到参加圈子的人
	public AppResult peopleList(HttpServletRequest request){
		AppResult r = new AppResult();
		Map<String,Object> m = new HashMap<String, Object>();
		Map<String, Object> mt = getPeoPle(request);
		List<Map<String, Object>> people = (List<Map<String, Object>>) mt.get("people");
		int maxSize = (Integer)mt.get("maxSize");
		m.put("people", people);
		m.put("peopleSize", people.size());
		m.put("maxSize", maxSize);
		r.setStatus(200);
		r.setResult(m);
		r.setMsg("success");
		return r;
	}
	//查询圈子的标签
	private static final String SELECT_ROUNDTIPS = "select TIPID from c_round_tip where ROUNDID=?";
	public Map<String,Object> getAdviceRoundInPublic(HttpServletRequest request){
		String roundId = (String)request.getAttribute("roundId");
		List<Map<String, Object>> tips = jt.queryForList(SELECT_ROUNDTIPS,roundId);
		StringBuffer buf = new StringBuffer("");
		Iterator<Map<String, Object>> it = tips.iterator();
		if(!it.hasNext()){
			buf.append("''");
		}
		while(it.hasNext()){
			Map<String, Object> next = it.next();
			int TIPID = (Integer) next.get("TIPID");
			buf.append("'"+TIPID+"'");
			if(it.hasNext()){
				buf.append(",");
			}
		}
		String SELECT_ADVICE_ROUND_PUBLICIN = "select ROUNDNAME,r.ROUNDID,RPATH,COUNT,DETAIL from C_ROUND_PHOTO p,C_ROUND r,C_ROUND_TIP t where p.RID=r.ROUNDID and r.ROUNDID=t.ROUNDID and r.STATUS = 0 and TIPID in ("+buf+") order by TOP DESC,COUNT DESC limit ?,?";
		String COUNT_ADVICE_ROUND_PUBLICIN = "select COUNT(*) from C_ROUND_PHOTO p,C_ROUND r,C_ROUND_TIP t where p.RID=r.ROUNDID and r.ROUNDID=t.ROUNDID and r.STATUS = 0 and TIPID in ("+buf+")";
		PagingUtil p = new PagingUtil(request);
		int maxSize = jt.queryForInt(COUNT_ADVICE_ROUND_PUBLICIN);
		/*
		 * 左侧推荐圈子数目 
		 * maxsize
		 */
		int[] limitCause = p.getLimitCause("advicePage", 3, maxSize);
		List<Map<String, Object>> adviceList = jt.queryForList(SELECT_ADVICE_ROUND_PUBLICIN,limitCause[0],limitCause[1]);
		Map<String,Object> m = new HashMap<String, Object>();
		m.put("adviceList", adviceList);
		m.put("maxSize", limitCause[2]);
		return m;
	}
	//获取共同圈子以分页形式
	private static final String GET_COMMON_ROUND_PAGING="select a.* from (select r.ROUNDID,r.ROUNDNAME,rp.RPATH,r.DETAIL from c_round r,c_round_attention ra,c_round_photo rp where r.STATUS=0 and ra.ROUNDID=r.ROUNDID and r.ROUNDID=rp.RID and ra.UID=?) as a cross join (select r.ROUNDID,r.ROUNDNAME,rp.RPATH,r.DETAIL from c_round r,c_round_attention ra,c_round_photo rp where r.STATUS=0 and ra.ROUNDID=r.ROUNDID and r.ROUNDID=rp.RID and ra.UID=?) as b on a.ROUNDID = b.ROUNDID limit ?,?";
	//获取共同圈子数目
	private static final String GET_COMMON_ROUND_COUNT="select count(a.ROUNDID) from (select r.ROUNDID,r.ROUNDNAME,rp.RPATH from c_round r,c_round_attention ra,c_round_photo rp where r.STATUS=0 and ra.ROUNDID=r.ROUNDID and r.ROUNDID=rp.RID and ra.UID=?) as a cross join (select r.ROUNDID,r.ROUNDNAME,rp.RPATH from c_round r,c_round_attention ra,c_round_photo rp where r.STATUS=0 and ra.ROUNDID=r.ROUNDID and r.ROUNDID=rp.RID and ra.UID=?) as b on a.ROUNDID = b.ROUNDID";
	//获取共同圈子标签名
	private static final String GET_COMMON_ROUND_TIPS="select TIPNAME,crt.TIPID from c_round_tip crt,c_tip ct where ct.TIPID=crt.TIPID and crt.ROUNDID=?";
	public AppResult getCommonRound(HttpServletRequest request){
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		String friendId = request.getParameter("friendId");
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		List<Map<String, Object>> commonRoundList = jt.queryForList(GET_COMMON_ROUND_PAGING,UID,friendId,from,to);
		Iterator<Map<String, Object>> it =commonRoundList.iterator();
		ArrayList commonRound = new ArrayList();
		while(it.hasNext()){
			Map next = it.next();
			String roundId=next.get("ROUNDID")+"";
			String roundName=(String) next.get("ROUNDNAME");
			String roundDetail=(String) next.get("DETAIL");
			String roundPhoto=(String) next.get("RPATH");
			List<Map<String, Object>> commonRoundTipsList = jt.queryForList(GET_COMMON_ROUND_TIPS,roundId);
			next.put("roundId",roundId);
			next.put("roundName",roundName);
			next.put("roundDetail",roundDetail);
			next.put("roundPhoto",roundPhoto);
			next.put("commonRoundTipsList",commonRoundTipsList);
			next.put("commonRoundTipsListSize",commonRoundTipsList.size());
			commonRound.add(next);
		}
		int All_record = jt.queryForInt(GET_COMMON_ROUND_COUNT,UID,friendId);
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		
		Map m = new HashMap();
		m.put("commonRoundList", commonRound);
		m.put("commonRoundListSize", commonRound.size());
		m.put("All_page", All_page);
		m.put("All_record", All_record);
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}
	
	
	/*	
	 * 分页搜索获取圈子信息
	 * 
	*/	
	//搜索圈子按照参与人数排列
	private static final String SEARCH_ROUND_BY_NAME="select r.ROUNDID,r.ROUNDNAME,r.DETAIL,r.COUNT,rp.RPATH from c_round r,c_round_photo rp where rp.RID=r.ROUNDID and r.ROUNDNAME like ? order by r.COUNT DESC limit ?,? ";
	//搜索圈子数目
	private static final String SEARCH_ROUND_COUNT_BY_NAME="select count(r.ROUNDID) from c_round r where  r.ROUNDNAME like ?";
	
	public AppResult getSearchRound(HttpServletRequest request){
		AppResult r = new AppResult();
		//String UID = (String) request.getSession().getAttribute("UID");
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		if(request.getParameter("roundName")==null){
			r.setMsg("error");
			r.setStatus(300);
			return r;
		}
		String roundNameStr ="%"+request.getParameter("roundName")+"%";
		System.out.println("roundNameStr====>"+roundNameStr);
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		List<Map<String, Object>> searchRoundList = jt.queryForList(SEARCH_ROUND_BY_NAME,roundNameStr,from,to);
		Iterator<Map<String, Object>> it =searchRoundList.iterator();
		ArrayList roundList = new ArrayList();
		while(it.hasNext()){
			Map next = it.next();
			String roundId=next.get("ROUNDID")+"";
			String roundName=(String) next.get("ROUNDNAME");
			String roundDetail=(String) next.get("DETAIL");
			String roundPhoto=(String) next.get("RPATH");
			List<Map<String, Object>> roundTipsList = jt.queryForList(GET_COMMON_ROUND_TIPS,roundId);
			next.put("roundId",roundId);
			next.put("roundName",roundName);
			next.put("roundDetail",roundDetail);
			next.put("roundPhoto",roundPhoto);
			next.put("roundTipsList",roundTipsList);
			next.put("roundTipsListSize",roundTipsList.size());
			roundList.add(next);
		}
		int All_record = jt.queryForInt(SEARCH_ROUND_COUNT_BY_NAME,roundNameStr);
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		
		Map m = new HashMap();
		m.put("roundList", roundList);
		m.put("roundListSize", roundList.size());
		m.put("All_page", All_page);
		m.put("All_record", All_record);
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}
}
